package com.wenson.behavior.iterator;

/**
 * @ClassName: DrivingRecorder
 * @Author: luowangcheng
 * @Date: 2024/6/15 12:40
 * @Description:
 */
public class DrivingRecorder {
    private int index = -1; //记录当前位置
    private String[] records = new String[10]; //最多记录10条视频

    public void append(String record) {
        if (index == 9) { //索引重置，从头覆盖
            index = 0;
        } else { //正常覆盖下一条
            index++;
        }
        records[index] = record;
    }

    public void display() { //循环数组并显示所有10条记录
        for (int i = 0; i < 10; i++) {
            System.out.println(i + ": " + records[i]);
        }
    }

    public void displayByOrder() { //按序从新到旧显示10条记录
        for (int i = index, loopCount = 0; loopCount < 10; i = i == 0 ? i = 9 : i - 1, loopCount++) {
            System.out.println(records[i]);
        }
    }
}
